package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.SecurityUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctQualityRulesBo;
import com.rzdata.poct.system.domain.business.po.PoctQualityRules;
import com.rzdata.poct.system.domain.business.po.PoctQualityRulesItem;
import com.rzdata.poct.system.domain.business.vo.PoctQualityRulesVo;
import com.rzdata.poct.system.mapper.PoctQualityRulesMapper;
import com.rzdata.poct.system.service.IPoctQualityRulesItemService;
import com.rzdata.poct.system.service.IPoctQualityRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctQualityRulesServiceImpl extends ServicePlusImpl<PoctQualityRulesMapper, PoctQualityRules, PoctQualityRulesVo> implements IPoctQualityRulesService {

    @Autowired
    private PoctQualityRulesMapper poctQualityRulesMapper;

    @Autowired
    IPoctQualityRulesItemService iPoctQualityRulesItemService;

    @Override
    public PoctQualityRulesVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctQualityRulesVo> queryPageList(PoctQualityRulesBo bo) {
        PagePlus<PoctQualityRules, PoctQualityRulesVo> result
            = pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctQualityRulesVo> queryList(PoctQualityRulesBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctQualityRules> buildQueryWrapper(PoctQualityRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctQualityRules> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotEmpty(bo.getStatus()), PoctQualityRules::getStatus, 1);
        lqw.eq(bo.getMaterialId() != null, PoctQualityRules::getMaterialId, bo.getMaterialId());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialNo()), PoctQualityRules::getMaterialNo, bo.getMaterialNo());
        lqw.like(StringUtils.isNotBlank(bo.getBatchNo()), PoctQualityRules::getBatchNo, bo.getBatchNo());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), PoctQualityRules::getMaterialName, bo.getMaterialName());
        return lqw;
    }

    @Override
    @Transactional
    public Boolean insertByBo(PoctQualityRulesBo bo) {
        PoctQualityRules add = BeanUtil.toBean(bo, PoctQualityRules.class);
        add.setCreateBy(SecurityUtils.getUsername());
        add.setCreateTime(new Date());
        add.setStatus(1);
        validEntityBeforeSave(add);
        Boolean result = save(add);
        if (ObjectUtil.isNotEmpty(bo.getItemList())) {
            insertRuleItem(add.getId(), bo.getItemList());
        }
        return result;
    }

    private void insertRuleItem(Long id, List<PoctQualityRulesItem> itemList) {
        if (ObjectUtil.isNotEmpty(itemList)) {
            for (PoctQualityRulesItem poctQualityRulesItem : itemList) {
                poctQualityRulesItem.setRuleId(id);
                QueryWrapper<PoctQualityRulesItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(PoctQualityRulesItem:: getRuleId, id);
                iPoctQualityRulesItemService.remove(queryWrapper);
            }
            iPoctQualityRulesItemService.saveBatch(itemList);
        }
    }

    @Override
    @Transactional
    public Boolean updateByBo(PoctQualityRulesBo bo) {
        PoctQualityRules update = BeanUtil.toBean(bo, PoctQualityRules.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(new Date());
        update.setStatus(1);
        Boolean result = updateById(update);
        if (ObjectUtil.isNotEmpty(bo.getItemList())) {
            insertRuleItem(update.getId(), bo.getItemList());
        }
        return result;
    }

    @Override
    public Boolean cancelById(Long id) {
        PoctQualityRules poctQualityRules = new PoctQualityRules();
        poctQualityRules.setStatus(0);
        poctQualityRules.setUpdateTime(new Date());
        poctQualityRules.setId(id);
        return updateById(poctQualityRules);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctQualityRules entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public TableDataInfo<PoctQualityRulesVo> queryPageRulesList(PoctQualityRules poctQualityRules) {
        Page<PoctQualityRules> objectPage = PageUtils.buildPage();
        return PageUtils.buildDataInfo(poctQualityRulesMapper.queryPageRulesList(objectPage, poctQualityRules));
    }


}
